﻿//-----------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microsoft.LibraryManagerDemo.Controls
{
    /// <summary>
    /// Represents a strongly typed circular list of objects that can be accessed by index. Provides method to move to the next item.
    /// </summary>
    /// <typeparam name="T">The type of values </typeparam>
    public class CircularList<T> : List<T>
    {
        /// <value>Index of current item.</value>
        private int _currentIndex = 0;

        /// <summary>
        /// Move to the next item in a circular manner.
        /// </summary>
        /// <returns>The next item in the circular list.</returns>
        public T Next()
        {
            ++_currentIndex;

            return Current;
        }

        /// <summary>
        /// Gets or sets current item.
        /// </summary>
        public T Current
        {
            get
            {
                if (Count == 0)
                {
                    return default(T);
                }

                if (_currentIndex >= Count)
                {
                    _currentIndex = 0;
                }

                return this[_currentIndex];
            }
            set
            {
                int indexOfValue = IndexOf(value);
                if (indexOfValue == -1)
                {
                    throw new ArgumentOutOfRangeException();
                }

                _currentIndex = indexOfValue;
            }
        }
    }
}
